<template>
  <div>
    <el-row>
      <el-col :span="10" :offset="6">
        <!-- 搜索框 -->
        <div style="margin-top: 15px;">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" @keyup.enter.native="getBookList" class="input-with-select" clearable @clear="getBookList">
            <el-button slot="append" icon="el-icon-search" @click="getBookList"></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <el-card :body-style="{ padding: '5px'}">
      <el-table :data="bookList" border stripe>
        <el-table-column type="index" label="序号"></el-table-column>
        <!-- <el-table-column prop="id" label="序号"></el-table-column> -->
        <el-table-column prop="title" label="书名" width="180">
        </el-table-column>
        <el-table-column prop="catalog" label="分类">
        </el-table-column>
        <el-table-column prop="score" label="评分">
        </el-table-column>
        <el-table-column prop="publisher" label="出版社">
        </el-table-column>
        <el-table-column prop="isbn13" label="isbn13">
        </el-table-column>
        <el-table-column prop="gmtCreate" label="创建时间">
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="removeById(scope.row.id)"
            >移除</el-button>
             <el-button
              type="primary"
              size="mini"
              @click="findMore(scope.row.id)"
            >更多</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 页码 -->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[6, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 查询参数对象
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 6
      },
      bookList: [],
      // 总数据条数
      total: 0,
      currentDate: new Date()
    }
  },
  created() {
    this.getBookList()
  },
  methods: {
    async getBookList() {
      const { data: res } = await this.$http.get('book/list', {
        params: this.queryInfo
      })
      if (res.code !== 200) {
        return this.$message.error('获取商品分类数据失败！')
      }
      this.bookList = res.data.list
      this.total = res.data.totalCount
      console.log('data', res.data)
      console.log(this.bookList)
    },
    // 监听每页条数改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      console.log('newSize', newSize)
      this.getBookList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getBookList()
    },
  // 根据id,删除商品
    async removeById(bookId) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该书籍, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$http.delete(`book/delete/${bookId}`)
      if (res.code !== 200) {
        return this.$message.error('删除失败失败！')
      }
      this.$message.success('删除商品成功！')
      this.getBookList()
    },
    findMore(bookId) {
      console.log(bookId)
      this.$router.push('/detail/' + bookId)
    }
  }
}
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-main {
  background-color: #f5f5f5;
}
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
.block {
  margin-top: 10px;
}
</style>
